from django.contrib.auth.models import AbstractUser
from django.db import models
from BYD_ERP.utils.base_model import BaseModel

# python ../../manage.py startapp erp_system

# 迁移基础：无。

# python ../../manage.py makemigrations erp_system
# python ../../manage.py migrate erp_system

# python ../../manage.py check

# 功能菜单的模型类:
class MenuModel(BaseModel):
    number = models.IntegerField('排序序号', blank=True, null=True)
    url = models.CharField('菜单访问的URL地址', max_length=256, blank=True, null=True)
    name = models.CharField('功能菜单的名字', max_length=50)
    # 不会真正把数据库中的数据删除，默认是0，如果是1，则表示删除
    delete_flag = models.CharField('删除的标记', max_length=1, default='0')
    parent = models.ForeignKey('self', blank=True, null=True, related_name='children', on_delete=models.CASCADE)

    class Meta:
        db_table = 't_menu'
        verbose_name = '功能菜单表'
        verbose_name_plural = verbose_name
        ordering = ['number']

    def __str__(self):
        return self.name



# 权限的模型类
class PermissionsModel(BaseModel):
    """
        权限的模型类，包含两个要素：资源（没有接口的父菜单，接口），操作
    """
    method_choices = (
        ('POST', '增'),
        ('DELETE', '删'),
        ('PUT', '改'),
        ('PATCH', '改局部'),
        ('GET', '查')
    )
    # method_choices = ('POST', 'PUT', 'XXX')
    """
        只要是choices参数的字段，如果想要获取对应的key和value，默认获取的是key,
        如果需要获取value，可以采用get_字段名_display()
        print(permission.method)   这样是获取key
        print(permission.get_method_display())  这样是获取value
    """

    name = models.CharField('权限名称', max_length=50)
    is_menu = models.BooleanField('是否为菜单', default=True)  # True就是父菜单，False就是子接口
    # 操作
    method = models.CharField('操作', max_length=8, blank=True, default='', choices=method_choices)
    path = models.CharField('访问URL地址', max_length=256, blank=True, null=True)
    desc = models.CharField('权限的描述', max_length=512, blank=True, null=True)
    menu = models.ForeignKey('MenuModel', blank=True, null=True, related_name='permissions_list',
                             on_delete=models.CASCADE)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 't_permissions'
        verbose_name = '权限表'
        verbose_name_plural = verbose_name
        ordering = ['id']


# 角色模型类
class RolesModel(BaseModel):
    """
        角色模型
    """
    # unique=True代表必填项
    name = models.CharField('角色名称', max_length=50, unique=True)
    permissions = models.ManyToManyField('PermissionsModel', db_table='t_roles_permissions', blank=True,
                                         verbose_name='角色的权限')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 't_roles'
        verbose_name = '角色表'
        verbose_name_plural = verbose_name
        ordering = ['id']


# 部门模型类
class DeptModel(BaseModel):
    """
        部门模型，
        一个用户只从属一个部门下
    """
    # unique=True代表必填项
    name = models.CharField('部门名称', max_length=50, unique=True)
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children', on_delete=models.CASCADE,
                               verbose_name='部门所在的父部门')
    address = models.CharField('部门所在地址', null=True, blank=True, max_length=256)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 't_dept'
        verbose_name = '部门表'
        verbose_name_plural = verbose_name
        ordering = ['id']


# ERP系统的用户模型类，默认采用django自带的User
class UserModel(AbstractUser, BaseModel):
    phone = models.CharField('手机号', max_length=11, unique=True, blank=True, null=True)
    real_name = models.CharField('真实姓名', max_length=64, blank=True, null=True)
    # 用户所有的角色
    roles = models.ManyToManyField('RolesModel', db_table='t_users_roles', blank=True, verbose_name='用户的角色')
    dept = models.ForeignKey('DeptModel', null=True, blank=True, on_delete=models.SET_NULL, verbose_name='用户所在部门')


    class Meta:
        db_table = 't_user'
        verbose_name = '系统用户表'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.username + ':' + self.real_name
